import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Auther: xiaofei
 * @Date: 2021/6/28 - 06 - 28 - 17:57
 * @Description: PACKAGE_NAME
 * @version: 1.0
 */
public class URLDown01 {
    //这是一个main方法，是程序的入口：
    public static void main(String[] args) throws Exception {
        // 1.下载地址
        URL url = new URL("https://m10.music.126.net/20210628183454/" +
                "46c7f62ebd23e7d2c9e31cc43ea49112/yyaac/035c/030f/0509/461ec3ad804e111741732c2afc4fdc18.m4a");
        // 2.连接到这个资源 HTTP
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream("music.m4a");
        byte[] buffer = new byte[1024];
        int len;
        while((len = inputStream.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }

        fos.close();
        inputStream.close();
        urlConnection.disconnect();
    }
}
